package com.sina.shop.controller;

import com.sina.shop.util.IpUtils;
import com.sina.shop.util.StringUtils;
import com.sina.shop.web.BaseController;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * 段浩杰   2017-09-22-15:26
 */
@Controller
public class IndexController extends BaseController {

    @RequestMapping(value = {"index", "/"})
    public ModelAndView index(HttpServletRequest request) {
        ModelAndView modelAndView = new ModelAndView("index");
        String res = (String) request.getSession().getAttribute("address");
        if (!StringUtils.isEmpty(res)) {
            return modelAndView;
        }
        String ip = IpUtils.getIpByRequest(request);
        String address = IpUtils.getAddresses("ip=" + ip, null);
        request.getSession().setAttribute("address", address);
        return modelAndView;
    }

    @RequestMapping(value = "change")
    public ModelAndView change(String address, HttpServletRequest request) {
        LOG.info("address:{}", address);
        ModelAndView modelAndView = new ModelAndView("commonView");
        request.getSession().setAttribute("address", address);
        return modelAndView;
    }

}
